package away3d.animators.utils ;


import away3d.animators.data.AnimationSequenceBase ;
import away3d.arcane ;



public class TimelineUtil
{
    
    
    private var _frame0         : Int ;
    private var _frame1         : Int ;
    private var _blendWeight    : Float ;
    
    
    
    public function TimelineUtil()
    {
        
        
        
    }
    
    
    public var frame0( get_frame0, null ) : Float ;
    
    
    public function get_frame0() : Float
    {
        
        return _frame0 ;
        
    }
    
    
    public var frame1( get_frame1, null ) : Float ;
    
    
    public function get_frame1() : Float
    {
        
        return _frame1 ;
        
    }
    
    
    public var blendWeight( get_blendWeight, null ) : Float ;
    
    
    public function get_blendWeight() : Float
    {
        
        return _blendWeight ;
        
    }
    
    
    /**
     * Calculates the frames between which to Interpolate.
     */
    public function updateFrames( time : Float, _activeSequence : AnimationSequenceBase )
    {
        
        var lastFrame       : Int, 
        var frame           : Int, 
        var nextFrame       : Int ;
        var dur             : Int, 
        var frameTime       : Int ;
        var durations                   = _activeSequence._durations ;
        var totalDuration               = _activeSequence._totalDuration ;
        var looping                     = _activeSequence.looping ;
        var numFrames                   = durations.length ;
        var w               : Float ;
        
        if( ( time > totalDuration || time < 0 ) && looping )
        {
            
            //TODO: %= won't work in haxe change
            time %= totalDuration ;
            if( time < 0 ) time += totalDuration ;
            
        }
        
        lastFrame = numFrames - 1 ;
        
        if( !looping && time > totalDuration - durations[ lastFrame ] )
        {
            
            _activeSequence.notifyPlaybackComplete() ;
            frame       = lastFrame ;
            nextFrame   = lastFrame ;
            w           = 0 ;
            
        }
        else if( _activeSequence._fixedFrameRate ) 
        {
            
            var t       = time/totalDuration * numFrames ;
            frame       = t ;
            nextFrame   = frame + 1 ;
            w           = t - frame ;
            
            if( frame == numFrames ) frame = 0 ;
            if( nextFrame >= numFrames ) nextFrame -= numFrames ;
            
        }
        else 
        {
            do
            {
                frameTime   = dur ;
                dur         += durations[ frame ] ;
                frame       = nextFrame ;
                
                if( ++nextFrame == numFrames ) 
                {
                    
                    nextFrame = 0 ;
                    
                }
                
            } 
            while( time > dur ) ;
            
            w = ( time - frameTime ) / durations[ frame ] ;
            
        }
        
        _frame0         = frame ;
        _frame1         = nextFrame ;
        _blendWeight    = w ;
        
    }
    
    
}